package com.javaeye.dengyin2000.gwtmodel.example.client;

import java.util.Date;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.javaeye.dengyin2000.gwtmodel.client.convert.SimpleConverterAdapter;
import com.javaeye.dengyin2000.gwtmodel.client.ui.PopulateForm;
import com.javaeye.dengyin2000.gwtmodel.client.ui.PopulateTextBox;
import com.javaeye.dengyin2000.gwtmodel.example.client.gwtr.ClassRegistrator;
import com.javaeye.dengyin2000.gwtmodel.example.client.pojo.Contact;
import com.javaeye.dengyin2000.gwtmodel.example.client.pojo.Person;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GWTModelExample implements EntryPoint {

  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {
	  ClassRegistrator.registerAll();
	  final DateTimeFormat format = DateTimeFormat.getFormat("yyyy-MM-dd");
	  final Person person = new Person();
	  person.setAddress("Guangdong GuangZhou sha tai lu F2-802");
	  person.setAge(new Integer(26));
	  person.setBirthday(format.parse("1983-1-15"));
	  person.setName("dengyin");
	  person.getMap().put("book", "ajax in action");
	  
	  person.setContact(new Contact());
	  person.getContact().setLinks(new String[]{"http://dengyin2000.javaeye.com"});
	  person.getContact().setTelNo("13760856034");
	  
	  final PopulateForm form = new PopulateForm(person);
	  final PopulateTextBox nameTB = new PopulateTextBox("name");
	  final PopulateTextBox addressTB = new PopulateTextBox("address");
	  final PopulateTextBox ageTB = new PopulateTextBox("age");
	  ageTB.setConverter(new SimpleConverterAdapter(){

				public Object toObject(String string) {
					return Integer.valueOf(string);
				}

				public String toString(Object value) {
					return value.toString();
				}
				  
			  });
	  final PopulateTextBox birthdayTB = new PopulateTextBox("birthday");
	  birthdayTB.setConverter(
			  new SimpleConverterAdapter(){

				public Object toObject(String string) {
					return format.parse(string);
				}

				public String toString(Object value) {
					return format.format(((Date)value));
				}
				 
			 }
		);
	  
	  final PopulateTextBox mapTB = new PopulateTextBox("map(book)");
	  final PopulateTextBox arrayTB = new PopulateTextBox("contact.links[0]");
	  
	  form.addWidget(ageTB).addWidget(arrayTB).addWidget(mapTB).addWidget(birthdayTB).addWidget(addressTB).addWidget(nameTB);
	  
	  Button b = new Button("Populate Value");
	  b.addClickListener(new ClickListener(){

		public void onClick(Widget sender) {
			form.populate();
			
			GWT.log("name: " + person.getName(), null);
			GWT.log("age: " + person.getAge(), null);
			GWT.log("birthday: " + format.format(person.getBirthday()), null);
			GWT.log("map: " + person.getMap().get("book"), null);
			GWT.log("address: " + person.getAddress(), null);
			GWT.log("contract array value: " + person.getContact().getLinks()[0], null);
		}
		  
	  });
	  
	  FlexTable table = new FlexTable();
	  table.setWidget(0, 0, new Label("Name:"));
	  table.setWidget(0, 1, nameTB);
	  table.setWidget(1, 0, new Label("Age::"));
	  table.setWidget(1, 1, ageTB);
	  table.setWidget(2, 0, new Label("Birthday:"));
	  table.setWidget(2, 1, birthdayTB);
	  table.setWidget(3, 0, new Label("Map value:"));
	  table.setWidget(3, 1, mapTB);
	  table.setWidget(4, 0, new Label("Address:"));
	  table.setWidget(4, 1, addressTB);
	  table.setWidget(5, 0, new Label("Contact array value:"));
	  table.setWidget(5, 1, arrayTB);
	  table.setWidget(6, 0, b);
	  
	  RootPanel.get().add(table);
  }
}
